var canshu=1;
var path = window.location.href;
var param = path.split("?");
var paramMap = param[param.length-1].split("=");

var color
var duankou='0'
var kehu='0'
var pingtai='0'
var CangName=""
var code=""
var warehouses=JSON.parse(sessionStorage.getItem("warehouses"))//获取仓库数据

$.each(warehouses.data,function(k,s){
    code +=s.warehouseCode+ ","
    CangName+=s.warehouseName+"/"

})
//去掉最后一个逗号(如果不需要去掉，就不用写)
if (code.length > 0) {
    code = code.substr(0, code.length - 1);
}
if (CangName.length > 0) {
    CangName = CangName.substr(0, CangName.length - 1);
}

// $("#putong").attr("checked", true);
// $("#qianyue").attr("checked", true);

$("#form-member").validate({
    rules: {
        title:"titleCheck",
        color:"colorCheck",
        lunbotu:"sequenceCheck"
    },
    //提交表单数据
    submitHandler:function(form) {

        //循环class，获取到每个check的选中状态

        if (pingtai=='0'){
            $("#putong").css('outline','1px solid red')
            $("#qianyue").css('outline','1px solid red')
            layer.msg("请配置平台", {icon: 6, time: 3000});
            return false
        }

        if(pingtai=='10'||pingtai=='10,20'){
            if (kehu=='0'){
                $("#fen").css('outline','1px solid red')
                $("#nofen").css('outline','1px solid red')
                layer.msg("请配置签约平台下的客户类型", {icon: 6, time: 3000});
                return false
            }
        }
        var price_types = "";
        var price_typeall=[]
        for (var f = 0; f < $("input:checkbox[name='price_typeall']:checked").length; f++) {
            price_typeall.push($("input:checkbox[name='price_typeall']:checked")[f].value)
        }
        if(pingtai==20){
            for (var h = 0; h < price_typeall.length; h++) {
                if (price_typeall[h] == 30) {
                    price_typeall.splice(h, 1);
                }
            }
        }
        for (var g = 0; g < price_typeall.length; g++) {
            price_types += price_typeall[g]+ ",";
        }
        //去掉最后一个逗号(如果不需要去掉，就不用写)
        if (price_types.length > 0) {
            price_types = price_types.substr(0, price_types.length - 1);
        }
        console.log(price_types)
        if (price_types==''){
            layer.msg("请配置价格类型", {icon: 6, time: 3000});
            return false
        }
        if (code==''){
            var warehouses=JSON.parse(sessionStorage.getItem("warehouses"))//获取仓库数据

            $.each(warehouses.data,function(k,s){
                var warecode= document.getElementById('yun'+s.warehouseCode+'')
                $(warecode).css('outline','1px solid red')

            })
            layer.msg("请配置区域", {icon: 6, time: 3000});
            return false
        }

        var formData = new FormData();
        formData.append("port",10);
        formData.append("different_customer_code",kehu);
        formData.append("price_type",price_types);
        formData.append("started_at",$("#logmin").val());
        formData.append("category_id",paramMap[1]);
        formData.append("end_in",$("#logmax").val());
        formData.append("image_url",$("#ordinaryUrl").val());
        formData.append("ordinary_image_url",$("#url").val());
        formData.append("title",$("#biaoti").val());
        formData.append("file",$("#file")[0].files[0]);
        formData.append("sequence",$("#lunbo").val());
        formData.append("created_name",JSON.parse(web.readCookie("loginData")).user.username);
        formData.append("created_by",JSON.parse(web.readCookie("loginData")).user.user_ID);
        /*平台和仓库*/
        formData.append("platform",pingtai);
        formData.append("warehouse_code",code);
        formData.append("warehouse_name",CangName);
        console.log($("#file")[0].files[0])
        canshu=canshu+1;
        if (canshu>2){
            return false;
        }else{
            $.ajax({
                url: root.sroreinter.save_resource_conf,
                cache: false,  //禁用缓存
                type: "POST",
                // 告诉jQuery不要去处理发送的数据
                processData : false,
                // 告诉jQuery不要去设置Content-Type请求头
                contentType : false,
                data: formData,
                success: function (data) {
                    console.log(data)
                    if (data.errorCode=="92"){
                        layer.msg('您添加的开始时间小于已存在数据的结束时间!', {icon: 6, time: 3000});
                    }
                    if (data.errorCode=="94"){
                        layer.msg(data.errorMessage, {icon: 6, time: 3000});
                    }
                    if(data.errorCode=="0") {
                        layer.msg('操作成功!', {icon: 6, time: 1000});
                        parent.queryDataFn();
                        setTimeout( function () {
                            cancelLayerNoRefreshFn();
                        },700);
                    }
                    if (data.errorCode=="93"){
                        layer.msg('开始时间不能大于结束时间,请重新查看', {icon: 6, time: 3000});
                        return
                    }
                }
            });
        }
    }
})

function changePingtai() {
    $("#putong").css('outline','')
    $("#qianyue").css('outline','')

    if ($("#qianyue").is(":checked")&&$("#putong").is(":checked")){
        $("#url").css('display','inline');
        $("#kehuleixing").css('display','inline');
        $("#ordinaryUrlXianShi").css('display','inline');
        pingtai='10'+","+'20'
        $("#price_typeall3").css('display','inline');
    }
    if ($("#qianyue").is(":checked")&&!$("#putong").is(":checked")){
        $("#url").css('display','none');
        $("#kehuleixing").css('display','inline');
        $("#ordinaryUrlXianShi").css('display','inline');
        pingtai='10'
        $("#price_typeall3").css('display','inline');
    }
    if ($("#putong").is(":checked")&&!$("#qianyue").is(":checked")){
        $("#url").css('display','inline');
        $("#kehuleixing").css('display','none');
        $("#ordinaryUrlXianShi").css('display','none');
        pingtai='20'
        $("#price_typeall3").css('display','none');
    }
    if (!$("#putong").is(":checked")&&!$("#qianyue").is(":checked")){
        $("#url").css('display','none');
        $("#kehuleixing").css('display','none');
        $("#ordinaryUrlXianShi").css('display','none');
        pingtai='0'
        $("#price_typeall3").css('display','none');
    }
}
function changeCang() {
    code=''
    CangName=''
    var warcode=""//id
    var warcodename=""//name
    var warehouses=JSON.parse(sessionStorage.getItem("warehouses"))//获取仓库数据
    $.each(warehouses.data,function(k,s){
        if(document.getElementById('yun'+s.warehouseCode+'').checked==true){//获取选中的id
            warcode+=document.getElementById('yun'+s.warehouseCode+'').value+ ","//逗号拼接获取到的所以id
            warcodename+=document.getElementById('yun'+s.warehouseCode+'').name+ "/"
        }
    })
    //去掉最后一位的逗号
    if (warcode.length > 0) {
        warcode = warcode.substr(0, warcode.length - 1);
    }
    if (warcodename.length > 0) {
        warcodename = warcodename.substr(0, warcodename.length - 1);
    }

    code=warcode
    CangName=warcodename
}
changeDuankou()
function changeDuankou() {
    $("#yidong").css('outline','')
    $("#pc").css('outline','')
     $('#urlXianZhi').html("PC端轮播图图片大小限制为740*460");
        $("#file").val("")
        $("#myImg").css("height","100px")
        $("#myImg").css("width","117.33px")
        $("#myImg").attr("src","../../../../static/h-ui.admin/images/20180302170728.png")
        duankou='10'


}

function changeKehu() {
    $("#fen").css('outline','')
    $("#nofen").css('outline','')

    if ($("#fen").is(":checked")&&$("#nofen").is(":checked")){
        kehu='10'+","+'20'
    }
    if ($("#fen").is(":checked")&&!$("#nofen").is(":checked")){
        kehu='10'
    }
    if ($("#nofen").is(":checked")&&!$("#fen").is(":checked")){
        kehu='20'
    }
    if (!$("#nofen").is(":checked")&&!$("#fen").is(":checked")){
        kehu='0'
    }
    console.log(kehu)
}

function changImg(e){

    var image = new Image();
    for (var i = 0; i < e.target.files.length; i++) {
        var file = e.target.files.item(i);
        if (!(/^image\/.*$/i.test(file.type))) {
            layer.msg('请上传图片!', {icon: 6, time: 2000});
            $("#file").val("")
            continue;//不是图片 就跳出这一次循环
        }
        //实例化FileReader API
        var freader = new FileReader();
        freader.readAsDataURL(file);
        freader.onload = function(e) {
            image.src =e.target.result ;
            image.onload = function() {
                if(file.size>5*1024*1024){
                    layer.msg('图片不得大于5M!', {icon: 6, time: 2000});
                    $("#file").val("")
                    canshu=1
                    return;
                }
                //判断此时选择的端口是app端还是pc端
                else if (this.width!=740||this.height!=460){
                        layer.msg('PC端图片大小限制为740*460!', {icon: 6, time: 2000});
                        r=1
                        $("#file").val("")
                        return;
                    } else {
                        $("#myImg").css({width:this.width/5+"px",height:this.height/5+"px",cursor:"pointer"});
                        $("#myImg").attr("src",e.target.result);
                    }

            };
        }
    }
}
function removeIframe(){
    var index = parent.layer.getFrameIndex(window.name);
    parent.layer.close(index);
}
$('#pic').click(function (event) {
    event.preventDefault()
    $('#file').click()
})
//价格类型全部
function pricetypes() {
    if($("#price_types").is(':checked')==true){
        var platformSelection=$("input:checkbox[name='price_typeall']")
        for(var i = 0; i < platformSelection.length; i++) {
            platformSelection[i].checked = true;
        }
    }else {
        var platformSelection=$("input:checkbox[name='price_typeall']")
        for(var i = 0; i < platformSelection.length; i++) {
            platformSelection[i].checked = false;
        }
    }
}
pricetypeall()
function pricetypeall() {
    var platformSelection=$("input:checkbox[name='price_typeall']")
    var platformSelection2=$("input:checkbox[name='price_typeall']:checked")
    if(platformSelection2.length==platformSelection.length){
        $("#price_types").prop("checked", true)
    }else {
        $("#price_types").prop("checked", false)
    }
}